/**
 * 
 */
package org.gov.vis.master.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import org.gov.vis.master.dto.GopalaMithraDto;
import org.gov.vis.util.DBConstants;
import org.gov.vis.util.DBStatements;
import org.gov.vis.util.DU;
import org.gov.vis.util.Util;

/**
 * @author Vishwanath
 *
 */
public class GopalaMithraDaoImpl implements GopalaMithraDao{

	/**
	 *Method called to either Add or Edit a GopalaMithra
	 */
	@Override
	public void addOrEditGopalaMithra(final GopalaMithraDto _gopalaMithraDto) throws Exception {
		Connection conn = null;
		PreparedStatement pStmt = null;
		
		try {
			conn = Util.getConnection();
			if(_gopalaMithraDto.getGplMthId() > 0){
				pStmt = conn.prepareStatement(DBStatements.EDIT_GOPALAMITHRA);
				pStmt.setString(1, _gopalaMithraDto.getCode());
				pStmt.setString(2, _gopalaMithraDto.getName());
				pStmt.setString(3, _gopalaMithraDto.getFatherName());
				pStmt.setString(4, DU.format(_gopalaMithraDto.getDob(), DBConstants.MYSQL_DATE_FORMAT));
				pStmt.setString(5, _gopalaMithraDto.getNativeVillage());
				pStmt.setString(6, _gopalaMithraDto.getMandal());
				pStmt.setString(7, _gopalaMithraDto.getDistrict());
				pStmt.setString(8, _gopalaMithraDto.getJurisdiction());
				pStmt.setString(9, _gopalaMithraDto.getHeadQuarter());
				pStmt.setString(10, _gopalaMithraDto.getHqMandal());
				pStmt.setString(11, _gopalaMithraDto.getHqDistrict());
				pStmt.setLong(12, _gopalaMithraDto.getGplMthId());
			}
			else{
				pStmt = conn.prepareStatement(DBStatements.ADD_GOPALAMITHRA);
				pStmt.setString(1, _gopalaMithraDto.getCode());
				pStmt.setString(2, _gopalaMithraDto.getName());
				pStmt.setString(3, _gopalaMithraDto.getFatherName());
				pStmt.setString(4, DU.format(_gopalaMithraDto.getDob(), DBConstants.MYSQL_DATE_FORMAT));
				pStmt.setString(5, _gopalaMithraDto.getNativeVillage());
				pStmt.setString(6, _gopalaMithraDto.getMandal());
				pStmt.setString(7, _gopalaMithraDto.getDistrict());
				pStmt.setString(8, _gopalaMithraDto.getJurisdiction());
				pStmt.setString(9, _gopalaMithraDto.getHeadQuarter());
				pStmt.setString(10, _gopalaMithraDto.getHqMandal());
				pStmt.setString(11, _gopalaMithraDto.getHqDistrict());
			}

			pStmt.executeUpdate();
		} catch (Exception e) {
			throw e;
		} finally {
			pStmt.close();
			conn.close();
		}
	}//end addOrEditDept

	/**
	 *Method called to Delete a GopalaMithra
	 */
	@Override
	public void deleteGopalaMithra(final long _gopalaMithraId) throws Exception {
		Connection conn = null;
		PreparedStatement pStmt = null;
		
		try {
			conn = Util.getConnection();
			pStmt = conn.prepareStatement(DBStatements.DELETE_GOPALAMITHRA);
			pStmt.setLong(1, _gopalaMithraId);
			pStmt.executeUpdate();
		} catch (Exception e) {
			throw e;
		} finally {
			pStmt.close();
			conn.close();
		}	

	}//end deleteDept

	/**
	 *Method called to get the list of GopalaMithras based on the passed GopalaMithra 
	 */
	@Override
	public List<GopalaMithraDto> getGopalaMithras(final GopalaMithraDto _gopalaMithraDto) throws Exception {
		Connection conn = null;
		PreparedStatement pStmt = null;
		ResultSet rsSet = null;
		List<GopalaMithraDto> _gopalaMithraList = null;
		try {
			conn = Util.getConnection();
			pStmt = conn.prepareStatement(DBStatements.SELECT_GOPALAMITHRAS);
			if(_gopalaMithraDto.getGplMthId() > 0){
				pStmt.setLong(1, _gopalaMithraDto.getGplMthId());
				pStmt.setLong(2, _gopalaMithraDto.getGplMthId());
			}
			else{
				pStmt.setLong(1, Long.MIN_VALUE);
				pStmt.setLong(2, Long.MAX_VALUE);
			}
			
			if(_gopalaMithraDto.getCode() != null && !_gopalaMithraDto.getCode().trim().equals("")){
				pStmt.setString(3, _gopalaMithraDto.getCode());
			}
			else{
				pStmt.setString(3, "%");
			}
			
			if(_gopalaMithraDto.getName() != null && !_gopalaMithraDto.getName().trim().equals("")){
				pStmt.setString(4, _gopalaMithraDto.getName());
			}
			else{
				pStmt.setString(4, "%");
			}
			
			rsSet = pStmt.executeQuery();
			
			_gopalaMithraList = populateGopalaMithras(rsSet);
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			pStmt.close();
			conn.close();
		}	

		return _gopalaMithraList;
	}//end getGopalaMithras


	/**
	 * Method called to populate the GopalaMithra records based on the passed result set
	 * @param _rsSet
	 * @return
	 */
	private List<GopalaMithraDto> populateGopalaMithras(final ResultSet _rsSet)throws Exception{
		List<GopalaMithraDto> _gopalaMithraList = null;
		GopalaMithraDto _gopalaMithraDto = null;
		
		while(_rsSet.next()){
			if(_rsSet.isFirst()){
				_gopalaMithraList = new ArrayList<GopalaMithraDto>();
			}
			//initialize the GopalaMithra
			_gopalaMithraDto = new GopalaMithraDto();
			_gopalaMithraDto.setGplMthId(_rsSet.getLong(DBConstants.COL_GOPALAMITHRA_ID));
			_gopalaMithraDto.setCode(_rsSet.getString(DBConstants.COL_GOPALAMITHRA_CODE));
			_gopalaMithraDto.setName(_rsSet.getString(DBConstants.COL_GOPALAMITHRA_NAME));
			_gopalaMithraDto.setFatherName(_rsSet.getString(DBConstants.COL_GOPALAMITHRA_FIRST_NAME));
			_gopalaMithraDto.setDob(_rsSet.getDate(DBConstants.COL_GOPALAMITHRA_DOB));
			_gopalaMithraDto.setNativeVillage(_rsSet.getString(DBConstants.COL_GOPALAMITHRA_NATIVE_VILLAGE));
			_gopalaMithraDto.setMandal(_rsSet.getString(DBConstants.COL_GOPALAMITHRA_MANDAL));
			_gopalaMithraDto.setDistrict(_rsSet.getString(DBConstants.COL_GOPALAMITHRA_DISTRICT));
			_gopalaMithraDto.setJurisdiction(_rsSet.getString(DBConstants.COL_GOPALAMITHRA_JURIDICTION));
			_gopalaMithraDto.setHeadQuarter(_rsSet.getString(DBConstants.COL_GOPALAMITHRA_HEADQUARTER));
			_gopalaMithraDto.setHqMandal(_rsSet.getString(DBConstants.COL_GOPALAMITHRA_HEADQUARTER_MANDAL));
			_gopalaMithraDto.setHqDistrict(_rsSet.getString(DBConstants.COL_GOPALAMITHRA_HEADQUARTER_DISTRICT));
			
			//add the current Object to the list
			_gopalaMithraList.add(_gopalaMithraDto);
			
		}
		return _gopalaMithraList;
	}//end populateGopalaMithras

	/**
	 * Method is called to search the GopalaMithras based on the gopalamithra.
	 */
	@Override
	public List<GopalaMithraDto> searchGopalaMithras(final GopalaMithraDto _gopalaMithraDto) throws Exception {
		Connection conn = null;
		PreparedStatement pStmt = null;
		ResultSet rsSet = null;
		List<GopalaMithraDto> _gopalaMithraList = null;
		try {
			conn = Util.getConnection();
			pStmt = conn.prepareStatement(DBStatements.SELECT_GOPALAMITHRAS);
			if(_gopalaMithraDto.getGplMthId() > 0){
				pStmt.setLong(1, _gopalaMithraDto.getGplMthId());
				pStmt.setLong(2, _gopalaMithraDto.getGplMthId());
			}
			else{
				pStmt.setLong(1, Long.MIN_VALUE);
				pStmt.setLong(2, Long.MAX_VALUE);
			}
			
			if(_gopalaMithraDto.getCode() != null && !_gopalaMithraDto.getCode().trim().equals("")){
				pStmt.setString(3, _gopalaMithraDto.getCode() + "%");
			}
			else{
				pStmt.setString(3, "%");
			}
			
			if(_gopalaMithraDto.getName() != null && !_gopalaMithraDto.getName().trim().equals("")){
				pStmt.setString(4, _gopalaMithraDto.getName() + "%");
			}
			else{
				pStmt.setString(4, "%");
			}
			
			rsSet = pStmt.executeQuery();
			
			_gopalaMithraList = populateGopalaMithras(rsSet);
		} catch (Exception e) {
			throw e;
		} finally {
			pStmt.close();
			conn.close();
		}	

		return _gopalaMithraList;
	}//end searchGopalaMithras

}//end class
